package com.example.weiting.huishenghuo.com.example.myapplication;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.TextView;

import com.example.weiting.huishenghuo.R;
import com.example.weiting.huishenghuo.utils.Message;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;


public class Add extends AppCompatActivity {
    List<Message> messageList;
    EditText et1,et2;
    TextView tv;
    RadioButton rb1,rb2;
    public final static int RESULT_CODE = 1;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add);
         et1 = (EditText) findViewById(R.id.editText3);
         et2 = (EditText) findViewById(R.id.editText4);
         tv = (TextView) findViewById(R.id.textView10);
         tv.setText(Calendar.getInstance().getTime().toString());
         rb1 = (RadioButton) findViewById(R.id.radioButton5);
         rb2 = (RadioButton) findViewById(R.id.radioButton6);

         //添加记账信息
        Button bt4 = (Button) findViewById(R.id.button4);
        bt4.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //获取其他页面传来的messageList。
                Intent intent = getIntent();
                Bundle args = intent.getBundleExtra("messages");
                messageList = (ArrayList<Message>) args.getSerializable("messageList");

                //增添新的记录
                if(rb2.isChecked()) {
                    messageList.add(new Message(et1.getText().toString(), Integer.parseInt(et2.getText().toString())));
                }else{
                    messageList.add(new Message(et1.getText().toString(), Integer.parseInt("-"+et2.getText().toString())));
                }
                messageList.get(messageList.size()-1).setDate(Calendar.getInstance().getTime());

                //返回结果集合messageList
                args.putSerializable("messageList",(Serializable)messageList);
                intent.putExtra("messages",args);
                setResult(RESULT_CODE, intent);
                finish();
            }
        });

        //点击返回按钮的事件
        Button bt5 = (Button) findViewById(R.id.button5);
        bt5.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //获取其他页面传来的messageList。
                Intent intent = getIntent();
                Bundle args = intent.getBundleExtra("messages");
                messageList = (ArrayList<Message>) args.getSerializable("messageList");

                //返回结果集合messageList
                args.putSerializable("messageList",(Serializable)messageList);
                intent.putExtra("messages",args);
                setResult(RESULT_CODE, intent);
                finish();
            }
        });

    }
}
